/* 
 * Copyright 2005-2008 Samuel Mello & Eduardo Schnell
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; version 2 of the License.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 */

/*
 * PreviewPanel.java
 *
 * Created on 26 de Dezembro de 2005, 23:21
 */

package datasoul.datashow;

import java.awt.Dimension;

import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import datasoul.config.DisplayControlConfig;
import datasoul.render.ContentManager;
import datasoul.serviceitems.ServiceItem;
import datasoul.util.ObjectManager;

/**
 *
 * @author  Administrador
 */
public class PreviewPanel extends javax.swing.JPanel implements ListSelectionListener {

    /**
	 * 
	 */
	private static final long serialVersionUID = 853165917594791935L;
	/**
     * Creates new form PreviewPanel
     */
    public PreviewPanel() {
        initComponents();
        serviceItemTable1.addTableListener(this);
        serviceItemTable1.setTitlePrefix(java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("PREVIEW:"));
        serviceItemTable1.setMediaControlsEnabled(false);

        cbAutoLive.setSelected(!DisplayControlConfig.getInstance().getAutomaticGoLiveBool());
        pnlContent.setVisible(!DisplayControlConfig.getInstance().getAutomaticGoLiveBool());

        Dimension size = new Dimension(ContentManager.PREVIEW_WIDTH, ContentManager.getInstance().getPreviewHeight());
        previewDisplayPanel1.setSize(size);
        previewDisplayPanel1.setPreferredSize(size);
        previewDisplayPanel1.setMinimumSize(size);
        previewDisplayPanel1.setMaximumSize(size);
        previewDisplayPanel1.initDisplay((int) size.getWidth(), (int) size.getHeight());
        ContentManager.getInstance().registerPreviewDisplay(previewDisplayPanel1.getContentDisplay());

    }


    public void previewItem(ServiceItem serviceItem){
        serviceItemTable1.setServiceItem(serviceItem, 0);
        serviceItem.previewItem();
        valueChanged(null);

        if (!cbAutoLive.isSelected()){
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    goLive(false);
                }
            });
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPreviewBox = new javax.swing.JPanel();
        cbAutoLive = new javax.swing.JCheckBox();
        pnlContent = new javax.swing.JPanel();
        serviceItemTable1 = new datasoul.serviceitems.ServiceItemTable();
        btnGoLive = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        previewDisplayPanel1 = new datasoul.render.SwingDisplayPanel();

        setDoubleBuffered(false);
        setLayout(new java.awt.BorderLayout());

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("datasoul/internationalize"); // NOI18N
        cbAutoLive.setText(bundle.getString("PREVIEW SELECTED ITEM BEFORE SHOWING IT ON OUTPUTS")); // NOI18N
        cbAutoLive.setFocusPainted(false);
        cbAutoLive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbAutoLiveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlPreviewBoxLayout = new javax.swing.GroupLayout(pnlPreviewBox);
        pnlPreviewBox.setLayout(pnlPreviewBoxLayout);
        pnlPreviewBoxLayout.setHorizontalGroup(
            pnlPreviewBoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPreviewBoxLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbAutoLive)
                .addContainerGap(67, Short.MAX_VALUE))
        );
        pnlPreviewBoxLayout.setVerticalGroup(
            pnlPreviewBoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(cbAutoLive)
        );

        add(pnlPreviewBox, java.awt.BorderLayout.NORTH);

        btnGoLive.setText(bundle.getString("START PRESENTATION")); // NOI18N
        btnGoLive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGoLiveActionPerformed(evt);
            }
        });

        jLabel8.setText(bundle.getString("PREVIEW:")); // NOI18N

        previewDisplayPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        previewDisplayPanel1.setPreferredSize(new java.awt.Dimension(160, 120));
        previewDisplayPanel1.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                previewDisplayPanel1previewDisplayResized(evt);
            }
        });

        javax.swing.GroupLayout previewDisplayPanel1Layout = new javax.swing.GroupLayout(previewDisplayPanel1);
        previewDisplayPanel1.setLayout(previewDisplayPanel1Layout);
        previewDisplayPanel1Layout.setHorizontalGroup(
            previewDisplayPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 156, Short.MAX_VALUE)
        );
        previewDisplayPanel1Layout.setVerticalGroup(
            previewDisplayPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 116, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout pnlContentLayout = new javax.swing.GroupLayout(pnlContent);
        pnlContent.setLayout(pnlContentLayout);
        pnlContentLayout.setHorizontalGroup(
            pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlContentLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(serviceItemTable1, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(previewDisplayPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGoLive))
                .addContainerGap())
        );
        pnlContentLayout.setVerticalGroup(
            pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlContentLayout.createSequentialGroup()
                .addGroup(pnlContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(serviceItemTable1, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                    .addGroup(pnlContentLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnGoLive)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(previewDisplayPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        add(pnlContent, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void previewDisplayPanel1previewDisplayResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_previewDisplayPanel1previewDisplayResized

}//GEN-LAST:event_previewDisplayPanel1previewDisplayResized

    private void cbAutoLiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbAutoLiveActionPerformed

        pnlContent.setVisible(cbAutoLive.isSelected());
        DisplayControlConfig.getInstance().setAutomaticGoLive(!cbAutoLive.isSelected());
        ObjectManager.getInstance().getDatasoulMainForm().updatePreviewHeight();

    }//GEN-LAST:event_cbAutoLiveActionPerformed

    private void btnGoLiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGoLiveActionPerformed
        goLive(false);
    }//GEN-LAST:event_btnGoLiveActionPerformed

    public void goLive(boolean startAtLastSlide){
        try{
            ObjectManager.getInstance().setBusyCursor();
            ServiceItem previewItem = ObjectManager.getInstance().getPreviewPanel().serviceItemTable1.getServiceItem();
            ObjectManager.getInstance().getLivePanel().showItem(previewItem, startAtLastSlide);
            ObjectManager.getInstance().getDatasoulMainForm().showDisplayControls();
        }finally{
            ObjectManager.getInstance().setDefaultCursor();
        }        
    }

    public void valueChanged(ListSelectionEvent e) {
        ContentManager cm = ContentManager.getInstance();
        cm.setSlidePreview( serviceItemTable1.getSlideText() );
        cm.setNextSlidePreview( serviceItemTable1.getNextSlideText() );
        cm.setActiveImagePreview(serviceItemTable1.getSlideImage());
        cm.setNextImagePreview(serviceItemTable1.getNextSlideImage());
        cm.updatePreview();
    }

    public int getPreferedHeight(){
        if (!cbAutoLive.isSelected()){
            return pnlPreviewBox.getHeight();
        }else{
            return pnlPreviewBox.getHeight() + (int) pnlContent.getPreferredSize().getHeight();
        }
    }

    public boolean isPreviewActive(){
        return !cbAutoLive.isSelected();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGoLive;
    private javax.swing.JCheckBox cbAutoLive;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel pnlContent;
    private javax.swing.JPanel pnlPreviewBox;
    private datasoul.render.SwingDisplayPanel previewDisplayPanel1;
    private datasoul.serviceitems.ServiceItemTable serviceItemTable1;
    // End of variables declaration//GEN-END:variables
    
}

